﻿using System.ComponentModel.DataAnnotations;

namespace BindTryParseMVC.Models
{
    /// <summary>
    /// 通过继承一个接口来实现验证Model中的属性值
    /// </summary>
    public class ValidatableMovie : IValidatableObject
    {
        private const int _classicYear = 1960;
        public int Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Title { get; set; } = null!;

        [DataType(DataType.Date)]
        [Display(Name = "Release Date")]
        public DateTime ReleaseDate { get; set; }

        [Required]
        [StringLength(1000)]
        public string Description { get; set; } = null!;

        [Range(0, 999.99)]
        public decimal Price { get; set; }

        public Gener Gener { get; set; }

        public bool Preorder { get; set; }

        // 通过继承接口实现验证发布日期是否符合要求
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if(Gener==Gener.Classic&& ReleaseDate.Year > _classicYear)
            {
                yield return new ValidationResult(
                    $"Classic movies must have a release year no later than {_classicYear}.",
                    new[] { nameof(ReleaseDate) }
                    );
            }
        }
    }
}
